﻿using System.Windows;
using System.Windows.Input;
using BestCode.Creds.View.Model;
using BestCode.Creds.View.Utils;
using BestCode.Creds.View.ViewModel;

namespace BestCode.Creds.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        VmPersonalData viewmodel = new VmPersonalData();

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = viewmodel;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            //if (viewmodel.IsPrimaryTemplate)
            //    viewmodel.GenerateCredentialsPrimary();
            //else
                viewmodel.GenerateCredentialsSecundary();

            viewmodel.GenerateCredentialsReverse();
        }

        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            viewmodel.SearchByFilter(txtFilter.Text);
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var obj = ((FrameworkElement)sender).DataContext as Dato;

            VwEditFoto editwindow = new VwEditFoto(obj);
            editwindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            editwindow.ShowDialog();
        }

        private void OpenOutputFolder(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.FileName = @"explorer";
            process.StartInfo.Arguments = viewmodel.OutputFolder;
            process.Start();
        }


        private void SetPicturesFolder(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.SelectedPath = viewmodel.PicturesFolder;
            var result = fbd.ShowDialog();

            if (result.ToString().Equals("OK"))
                viewmodel.PicturesFolder = fbd.SelectedPath;

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.SelectedPath = viewmodel.OutputFolder;
            var result = fbd.ShowDialog();

            if (result.ToString().Equals("OK"))
                viewmodel.OutputFolder = fbd.SelectedPath;
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            int count = viewmodel.EnabledItems.Count;
            MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Esta apunto de imprimir " + count + " credenciales en la impresora " + ConfigReader.Current.Config.DefaultPrinterName + ", \nesta seguro de continuar?  ", "Confirm", System.Windows.MessageBoxButton.YesNo);
            if (messageBoxResult == MessageBoxResult.Yes)
            {
                viewmodel.SendToPrinter();
            }
        }

    }

}
